package zerous.taphero;

import android.app.*;
import android.os.*;
import java.util.*;
import android.widget.*;
import android.content.*;
import zerous.taphero.util.*;
import android.view.*;
import android.widget.AdapterView.*;

import zerous.taphero.util.Profile.*;

public class CharacterSelectActivity extends Activity implements OnItemClickListener, OnItemLongClickListener
{
	
	List<String> heros = new ArrayList<String>();
	public static Profile profile;
	ArrayAdapter ada;
	ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select);

		list = (ListView)findViewById(R.id.selectListView1);
		list.setOnItemLongClickListener(this);
		list.setOnItemClickListener(this);
		
		heros = new ArrayList<String>();
	}

	@Override
	protected void onResume() {
		super.onResume();
		profile = new Profile(IOHelper.readData(this, ".profile"));
		for (DataGroup g:profile.dataGroups) {
			heros.add(g.name);
		}
		ada = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, heros);
		list.setAdapter(ada);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO: Implement this method
		MenuInflater inf = getMenuInflater();
		inf.inflate(R.menu.select_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case R.id.createHero: {
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("新建角色");
				final View l = getLayoutInflater().inflate(R.layout.new_hero, null);
				final RadioGroup group = (RadioGroup)l.findViewById(R.id.heroKinds);
				builder.setView(l);
				builder.setNegativeButton("关闭", null);
				builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface i, int b) {
						String heroName = ((EditText)l.findViewById(R.id.inputName)).getEditableText().toString();
						heros.add(heroName);
						ada.notifyDataSetChanged();
						Profile.DataGroup newHero = new Profile.DataGroup();
						newHero.name = heroName;
						newHero.putInt("exp", 0);
						newHero.putInt("level", 1);
						switch(group.getCheckedRadioButtonId()) {
							case R.id.warrior:
								newHero.putInt("kind", Hero.WARRIOR);
								break;
								
							case R.id.wizard:
								newHero.putInt("kind", Hero.WIZARD);
								break;
						}
						newHero.putInt("skill1", 0);
						newHero.putInt("skill2", 0);
						newHero.putInt("skill3", 0);
						newHero.putInt("skill4", 0);
						newHero.datas.add(new Data<String>("storage", "_"));
						profile.add(newHero);
					}
				});
				builder.show();
				break;
			}
		}
		return super.onOptionsItemSelected(item);
	}
	
	int position;
	@Override
	public boolean onItemLongClick(AdapterView<?> a, View v, int pos, long l) {
		position = pos;
		AlertDialog.Builder builder = new AlertDialog.Builder(CharacterSelectActivity.this);
		builder.setTitle("删除角色");
		builder.setMessage("确定要删除"+heros.get(pos)+"吗？");
		builder.setNegativeButton("不了", null);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface i, int b) {
					heros.remove(position);
					
					ada.notifyDataSetChanged();
					
					profile.remove(position);
				}
			});
		builder.show();
		ada.notifyDataSetChanged();
		return true;
	}

	@Override
	protected void onPause() {
		super.onPause();
		IOHelper.writeCharacters(this, heros);
		heros.clear();
		IOHelper.writeData(this, ".profile", profile.getDataSrc().getBytes());
	}

	@Override
	public void onItemClick(AdapterView<?> l, View v, int pos, long id) {
		String name = (String)l.getItemAtPosition(pos);
		Intent intent = new Intent(this, MainActivity.class);
		intent.putExtra("heroName", name);
		startActivity(intent);
		finish();
	}
	
	public boolean checkIsRepeated(String name) {
		for(String s:heros) {
			if(name.equals(s))
				return true;
		}
		return false;
	}
}
